﻿using System;
using System.Collections.Generic;
using System.Web;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;
using OneMG.Phoenix.Kernel.Util;
using OneMG.Phoenix.Kernel.Enum;

namespace OneMG.Phoenix.AdminSite.Views.New
{

    public class Save : IHttpHandler
    {
        INewsService INews = new NewsService();

        public void ProcessRequest(HttpContext context)
        {

            int actionType = 0;
            int.TryParse(context.Request.Form["action"], out actionType);
            string action = "";
            switch ((ActionType)actionType)
            {
                case ActionType.Insert:
                    {
                        Add(context);
                        action = "添加成功";
                    }
                    break;
                case ActionType.Delete:
                    {
                        delete(context);
                        action = "删除成功";
                    }
                    break;
                case ActionType.Modify:
                    {
                        update(context);
                        action = "修改成功";
                    }
                    break;

            }

            JsonResult.Result(context, JsonMessageStatus.OK, action);

        }


        private void Add(HttpContext context)
        {
            NewsModel model = new NewsModel()
            {
                Title = HttpContext.Current.Server.UrlDecode(context.Request.Form["title"]),
                Summary = HttpContext.Current.Server.UrlDecode(context.Request.Form["summary"]),
                Description = HttpContext.Current.Server.UrlDecode(context.Request.Form["description"]),
                CateID = int.Parse(context.Request.Form["sltCategory"]),
                IsTop = true,
                IsShow = true,
                PublishTime = DateTime.Now,
                CreateUser = 1,
                UpdateTime = DateTime.Now,
                UpdateUser = 1
            };

            int userID = INews.AddNews(model);

        }

        private void update(HttpContext context)
        {
            int newsid = 0;
            int.TryParse(context.Request.Form["newsid"], out newsid);

            NewsModel model = INews.GetByNewsID(newsid);

            model.Title = HttpContext.Current.Server.UrlDecode(context.Request.Form["title"]);
            model.Summary = HttpContext.Current.Server.UrlDecode(context.Request.Form["summary"]);
            model.Description = HttpContext.Current.Server.UrlDecode(context.Request.Form["description"]);
            model.CateID = int.Parse(context.Request.Form["sltCategory"]);
            model.IsTop = true;
            model.IsShow = true;
            model.UpdateTime = DateTime.Now;
            model.UpdateUser = 0;
            INews.UpdateByNewsID(model);
        }

        private void delete(HttpContext context)
        {
            int newsid = 0;
            int.TryParse(context.Request.Form["newsid"], out newsid);
            INews.DeleteByNewsID(newsid);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
